package com.baselibrary.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.baselibrary.utils.ContextUtils;
import com.baselibrary.utils.HKLogUtils;

public class DBHelper extends SQLiteOpenHelper {

    private final static String DB_NAME = "Hankol.db";
    private final static int DB_VERSION = 2;
    public final static String TABLE_NAME = "search_history";
    public final static String TABLE_NAME4MATERIAL = "search_history_material";
    public final static String TABLE_NAME_ORDER = "tab_search_history_order";
    public final static String TABLE_NAME_MANAGER_ORDER = "tab_search_history_manager_order";
    public final static String TABLE_MATERIAL_DOWNLOAD = "table_material_download";
    public final static String TIMMELINE_TABLE_NAME = "timeline_revision";
    private static volatile DBHelper sInstance;


    public DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public synchronized static DBHelper getInstance() {
        if (sInstance == null) {
            synchronized (DBHelper.class) {
                if (sInstance == null) {
                    sInstance = new DBHelper(ContextUtils.getCurApplication());
                }
            }
        }
        return sInstance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createChatTable2(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        HKLogUtils.e(oldVersion+"-----数据库onUpgrade----------"+newVersion);
        if (oldVersion==1&&newVersion==2) {
            //搜索订单
            createTab4SearchOrder(db);
            createTab4SearchManagerOrder(db);
        }
    }

    private void createChatTable2(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE search_history (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "userId VARCHAR," +
                "search_content VARCHAR)");
        //素材库相关
        createTab4Material(db);
        //搜索订单
        createTab4SearchOrder(db);
        createTab4SearchManagerOrder(db);

        db.execSQL("CREATE TABLE timeline_revision (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "userId VARCHAR," +
                "time VARCHAR," +
                "showType VARCHAR," +
                "title VARCHAR," +
                "content VARCHAR," +
                "address VARCHAR," +
                "provinceId VARCHAR," +
                "cityId VARCHAR," +
                "districtId VARCHAR," +
                "tagstr VARCHAR," +
                "tagsid VARCHAR," +
                "images VARCHAR," +
                "cover VARCHAR," +
                "video VARCHAR," +
                "friendsIdList VARCHAR," +
                "friendsNameList VARCHAR," +
                "status VARCHAR)");//0草稿 -1发布失败
    }

    private void createTab4Material(SQLiteDatabase db) {
        //素材库历史搜索
        db.execSQL("CREATE TABLE " + TABLE_NAME4MATERIAL + " (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "userId VARCHAR," +
                "search_content VARCHAR)");
        //素材库下载列表
        db.execSQL("CREATE TABLE " + TABLE_MATERIAL_DOWNLOAD + " (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "userId VARCHAR," +
                "url VARCHAR," +
                "fileName VARCHAR," +
                "fileIcon VARCHAR," +
                "path VARCHAR," +
                "time VARCHAR," +
                "type VARCHAR," +
                "downloadStatus VARCHAR," +
                "total VARCHAR," +
                "progress VARCHAR)");
    }
    private void createTab4SearchOrder(SQLiteDatabase db) {
        //订单历史搜索
        db.execSQL("CREATE TABLE " + TABLE_NAME_ORDER + " (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "userId VARCHAR," +
                "search_content VARCHAR)");

    }
    private void createTab4SearchManagerOrder(SQLiteDatabase db) {
        //经营者订单历史搜索
        db.execSQL("CREATE TABLE " + TABLE_NAME_MANAGER_ORDER + " (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "userId VARCHAR," +
                "search_content VARCHAR)");

    }

}